package com.ita.tba.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.tba.po.Dish;
import com.ita.tba.po.DishCache;
import com.ita.tba.po.OrderList;
import com.ita.tba.service.Controller;
import com.ita.tba.service.impl.ControllerImpl;

/**
 * Servlet implementation class ShowOrderListServlet
 */
public class ShowOrderListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Controller controller = new ControllerImpl();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowOrderListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String email = (String) session.getAttribute("email");
		
		Map<Integer, Dish> dishCache = new HashMap<Integer, Dish>();
		dishCache = DishCache.dishCache;
		
		int uid = controller.getUid(email);
		List<OrderList> olist= new ArrayList<OrderList>();
		olist = controller.getOrderList(uid);
		
		
		
		List<OrderList> dnameNumList = new ArrayList<OrderList>();
		for (OrderList orderList : olist) {
			Map<String, Integer> dnameNum = new HashMap<String, Integer>();
			for (Integer did : orderList.getDidNum().keySet()) {
				dnameNum.put(dishCache.get(did).getDname(), orderList.getDidNum().get(did)); 
				
			}
			orderList.setDnameNum(dnameNum);
			dnameNumList.add(orderList);
		}
		for (OrderList orderList : dnameNumList) {
			System.out.println(orderList.getDnameNum());
		}
		
		if (olist != null) {
			request.setAttribute("dnameNumList", dnameNumList);
		}
		
		request.getRequestDispatcher("/jsp/orderlist.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
